/*
 * Copyright © 2018 www.noark.xyz All Rights Reserved.
 *
 * 感谢您选择Noark框架，希望我们的努力能为您提供一个简单、易用、稳定的服务器端框架 ！
 * 除非符合Noark许可协议，否则不得使用该文件，您可以下载许可协议文件：
 *
 *        http://www.noark.xyz/LICENSE
 *
 * 1.未经许可，任何公司及个人不得以任何方式或理由对本框架进行修改、使用和传播;
 * 2.禁止在本项目或任何子项目的基础上发展任何派生版本、修改版本或第三方版本;
 * 3.无论你对源代码做出任何修改和改进，版权都归Noark研发团队所有，我们保留所有权利;
 * 4.凡侵犯Noark版权等知识产权的，必依法追究其法律责任，特此郑重法律声明！
 */
package xyz.noark.network.codec.shuttle;

import io.netty.buffer.ByteBuf;
import io.netty.handler.codec.ByteToMessageDecoder;
import xyz.noark.core.lang.ByteArray;
import xyz.noark.core.lang.ImmutableByteArray;
import xyz.noark.core.network.NetworkPacket;
import xyz.noark.core.network.NetworkProtocol;
import xyz.noark.network.codec.AbstractPacketCodec;
import xyz.noark.network.codec.rpc.RpcPacket;
import xyz.noark.network.rpc.RpcReqProtocol;
import xyz.noark.network.rpc.RpcSerializer;
import xyz.noark.network.rpc.shuttle.ShuttleRelayProtocol;
import xyz.noark.network.util.ByteBufUtils;
import xyz.noark.network.util.CodecUtils;

import java.io.Serializable;
import java.util.Collection;

/**
 * Shuttle协议编解码器
 *
 * @author 小流氓[176543888@qq.com]
 * @since 4.0
 */
public class ShuttlePacketCodec extends AbstractPacketCodec {
    private final RpcSerializer serializer;
    private final ShuttlePacketCodecFactory codecFactory;

    public ShuttlePacketCodec() {
        this(defaultSerializer());
    }

    public ShuttlePacketCodec(RpcSerializer serializer) {
        this.serializer = serializer;
        this.codecFactory = new ShuttlePacketCodecFactory(serializer);
    }

    public ShuttlePacketCodecFactory getCodecFactory() {
        return codecFactory;
    }

    @Override
    public <T> T decodeProtocol(ByteArray bytes, Class<T> klass) {
        return CodecUtils.deserialize(bytes.array(), klass, serializer);
    }

    @Override
    public ByteArray encodePacket(NetworkProtocol networkProtocol) {
        // Shuttle协议
        if (networkProtocol instanceof ShuttleRelayProtocol shuttleRelayProtocol) {
            Collection<Serializable> playerIdList = shuttleRelayProtocol.getPlayerIdList();
            if (playerIdList.size() == 1) {
                //return ShuttlePacketCodecFactory.encodeShuttlePacket(ShuttleType.FORWARD, playerIdList.shuttleProtocol);
            } else {
                return ShuttlePacketCodecFactory.encodeShuttlePacket(shuttleRelayProtocol);
            }


        }
        // RPC之请求协议
        else if (networkProtocol instanceof RpcReqProtocol rp) {
            return codecFactory.encodeRpcPacket(ShuttleType.RPC_REQUEST, rp.getReqId(), networkProtocol);
        }
        // RPC之响应协议 = 网络协议中有请求Rpc请求封包，那这个协议必然就是Rpc的响应
        else if (networkProtocol.getPacket() != null && networkProtocol.getPacket() instanceof RpcPacket rp) {
            return codecFactory.encodeRpcPacket(ShuttleType.RPC_RESPONSE, rp.getReqId(), networkProtocol);
        }

        // 其他类型...
        return null;
    }

    @Override
    public ShuttlePacketLengthEncoder lengthEncoder() {
        return new ShuttlePacketLengthEncoder();
    }

    @Override
    public ByteToMessageDecoder lengthDecoder() {
        return new ShuttlePacketLengthDecoder(this);
    }

    @Override
    public NetworkPacket decodePacket(ByteBuf byteBuf) {
        // RPC请求 : 包长(4) | 类型(1) | 协议编号(4) | 请求ID(4) | 标记位置(1) | 协议类型(1) | 协议
        RpcPacket packet = new RpcPacket();
        packet.setLength(byteBuf.readInt());
        byteBuf.skipBytes(1);
        packet.setOpcode(byteBuf.readInt());
        packet.setReqId(byteBuf.readInt());
        byteBuf.skipBytes(2);
        byte[] bytes = ByteBufUtils.readBytes(byteBuf);

       /* // 压缩标识，0=不压缩，1=GZip压缩,2=XX压缩
        if (compressFlag == 1) {
            try {
                bytes = GzipUtils.uncompress(bytes);
            } catch (IOException ignored) {
                // 出错了就不解了
            }
        }*/

        packet.setByteArray(new ImmutableByteArray(bytes));
        return packet;
    }
}